{% extends 'warehouse/base.html' %}

{% block title %}工资计算{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <h1>工资计算</h1>
        
        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
        
        <div class="card">
            <div class="card-header">
                <h3>选择计算日期范围</h3>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="{{ form.start_date.id_for_label }}" class="form-label">{{ form.start_date.label }}</label>
                            {{ form.start_date }}
                            {% if form.start_date.errors %}
                                <div class="alert alert-danger mt-2">{{ form.start_date.errors }}</div>
                            {% endif %}
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="{{ form.end_date.id_for_label }}" class="form-label">{{ form.end_date.label }}</label>
                            {{ form.end_date }}
                            {% if form.end_date.errors %}
                                <div class="alert alert-danger mt-2">{{ form.end_date.errors }}</div>
                            {% endif %}
                        </div>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">计算工资</button>
                </form>
            </div>
        </div>
        
        {% if wage_data %}
        <div class="card mt-4">
            <div class="card-header">
                <h3>{{ start_date|date:"Y年m月d日" }} 至 {{ end_date|date:"Y年m月d日" }} 工资计算结果</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>员工</th>
                                <th>任务类型</th>
                                <th>次数</th>
                                <th>总数量</th>
                                <th>单价</th>
                                <th>工资</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for username, data in wage_data.items %}
                                {% for task_type, task_data in data.tasks.items %}
                                <tr>
                                    {% if forloop.first %}
                                    <td rowspan="{{ data.tasks|length }}">{{ username }}</td>
                                    {% endif %}
                                    <td>
                                        {% if task_type == 'inbound' %}
                                            入库
                                        {% elif task_type == 'outbound' %}
                                            出库
                                        {% elif task_type == 'inventory' %}
                                            盘点
                                        {% endif %}
                                    </td>
                                    <td>{{ task_data.count }}</td>
                                    <td>{{ task_data.total_quantity }}</td>
                                    <td>¥{{ task_data.unit_price }}</td>
                                    <td>¥{{ task_data.wage|floatformat:2 }}</td>
                                </tr>
                                {% endfor %}
                                <tr class="table-info">
                                    <td colspan="5"><strong>{{ username }} 总工资</strong></td>
                                    <td><strong>¥{{ data.total_wage|floatformat:2 }}</strong></td>
                                </tr>
                            {% empty %}
                            <tr>
                                <td colspan="6">该日期范围内无工资记录</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        {% endif %}
        
        <div class="mt-3">
            <a href="{% url 'warehouse:wage_list' %}" class="btn btn-secondary">返回工资记录</a>
            <a href="{% url 'warehouse:dashboard' %}" class="btn btn-secondary">返回仪表盘</a>
        </div>
    </div>
</div>
{% endblock %}